package registnet.base.summary.delinquency;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import registnet.base.CourseTaken;
import registnet.base.Form5;
import registnet.base.source.Form5Source;
import registnet.base.student.Student;
import registnet.dao.StudentPeer;
import resources.Constants;

public class WarningDelinquencySource implements IScholarshipDelinquencySource {

	private int year;
	private int semester;
	private WarningDelinquency delinquencyList;
	
	public WarningDelinquencySource(int semester, int year){
		this.year = year;
		this.semester = semester;
		this.load();
	}
	
	public void load(){
		this.delinquencyList = new WarningDelinquency(this.semester, this.year);
		StudentPeer sp = new StudentPeer();
		List<registnet.dao.Student> students = sp.getAllStudents();
		for(int i=0; i<students.size(); i++){
			try{
				registnet.dao.Student DAOStudent = students.get(i);
				Student student = new Student(DAOStudent.getStudentNumber());
				student.setFirstName(DAOStudent.getFirstName());
				student.setMiddleName(DAOStudent.getMiddleName());
				student.setLastName(DAOStudent.getLastName());
				student.setSex(DAOStudent.getSex());
				student.setCollegeCode(DAOStudent.getCurriculum().getDegreeCourse().getCollege().getCollegeCode());
				student.setDegreeProgram(DAOStudent.getCurriculum().getDegreeCourse().getDegreeCode());
				Form5 form5 = new Form5Source(student.getStudentNumber(), this.semester, this.year).getForm5();
				double courseCount = 0;
				double failedCount = 0;
				HashMap<String, CourseTaken> courses = form5.getAllCourses();
				Iterator<String> iter = courses.keySet().iterator();
				while(iter.hasNext()){
					CourseTaken course = courses.get(iter.next());
					if (course.getGrade() != Constants.MAP_GRADE_KEY.get("DRP.") && course.getGrade() != Constants.MAP_GRADE_KEY.get("INC.") && course.getGrade() != Constants.MAP_GRADE_KEY.get("4")){
						if (course.getGrade() == Constants.MAP_GRADE_KEY.get("5") || course.getGrade() == Constants.MAP_GRADE_KEY.get("U"))
							failedCount += 1;
						courseCount++;
					}
				}
				double percentage = failedCount/courseCount;
				
				if (percentage >= (Double)0.25 && percentage < (Double)0.50)
					this.delinquencyList.add(student, Constants.WARNING);
				
			}catch(Exception e){}
		}
		
	}
	
	public IScholarshipDelinquency getDelinquencyList(){
		return this.delinquencyList;
	}
	
}
